package com.hyyt.shopjsp.store.info.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.hyyt.shopjsp.product.pojo.ShopProductInfo;
import com.hyyt.shopjsp.product.service.IShopProductInfoService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.store.info.pojo.ShopShopInfo;
import com.hyyt.shopjsp.store.info.pojo.ShopShopCategory;
import com.hyyt.shopjsp.store.info.service.IShopShopInfoService;
import com.hyyt.shopjsp.store.info.service.IShopShopCategoryService;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.util.json.FastJsonUtils;

/**
 * Copyright (c) 2017 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
 * @author Wang Youchun on 2017/04/24 0024下午 15:26.
 */

@Controller
@RequestMapping("/back/selfSupportShopInfo")
public class SelfSupportShopShopInfoController extends BaseController{
	
	private Logger log = Logger.getLogger(SelfSupportShopShopInfoController.class);
	private String basePath="/back/shop/selfSupportShopInfo/";
	@Resource
	private IShopShopInfoService shopShopInfoService;//店铺service
	@Resource
	private IShopShopCategoryService shopShopCategoryService;//店铺分类service
	@Resource
	private IShopProductInfoService shopProductInfoService;//商品service

	/**
	 * 进入自营店铺管理
	 * @params params
	 * @return
	 */
	@RequestMapping("gotoSelfSupportShopInfoPage")
	public String gotoSelfSupportShopInfoPage(Map<String, Object> params){
		List<ShopShopCategory> shopCategoryList=shopShopCategoryService.queryShopCategoryList(params);
        params.put("shopCategoryList", shopCategoryList);
		return basePath+"shopInfoList";
	}

	/**
	 * 查询自营SHOP_店铺
	 * @params page
	 * @params shopName			店铺名称
	 * @params customerName		店铺管理员名称
	 * @params isClose			是否关闭：	0:未关闭、1：已关闭
	 * @params shopCategoryId	店铺类型ID
	 * @params isPass			审核状态：	1：待审核、2：未通过、3：已通过
	 */
	@RequestMapping(value="listShopInfo",method=RequestMethod.POST)
	@ResponseBody
	public void listShopInfo(String shopName,String customerName,String isClose,String shopCategoryId,String isPass,Page page, HttpServletResponse response){
		Map<String, Object> params = new HashMap<>();
		if(Utils.objectIsNotEmpty(shopName)){
			params.put("shopName", shopName.trim());
		}
		if(Utils.objectIsNotEmpty(customerName)){
			params.put("customerName", customerName.trim());
		}
		if(Utils.objectIsNotEmpty(isClose)){
			params.put("isClose", isClose);
		}
		if(Utils.objectIsNotEmpty(isPass)){
			params.put("isPass", isPass);
		}
		if(Utils.objectIsNotEmpty(shopCategoryId)){
			params.put("shopCategoryId", shopCategoryId);
		}
		params.put("sortColumns", "shopInfoId desc");
		int totalRecordCount =shopShopInfoService.countSelfSupportShopInfo(params);
		page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
		List<ShopShopInfo> shopInfoListMap = shopShopInfoService.querySelfSupportShopInfoList(params, page.getPageRecordBeginIndex(), page.getPageSize());
		params.clear();
		params.put("total", totalRecordCount);// total键 存放总记录数，必须的
		params.put("rows", shopInfoListMap);// rows键 存放每页记录 list
		ResultMsg.create(params, response);  //把内容发送到页面
	}

	/**
	 * 根据店铺id获得对象
	 * @params request
	 * @return
	 */
	@RequestMapping(value="getShopInfoById",method=RequestMethod.POST)
	@ResponseBody
	public ResultMsg getShopInfoById(String shopInfoId){
		if(Utils.objectIsNotEmpty(shopInfoId)){
			ShopShopInfo shopShopinfo = shopShopInfoService.loadShopShopInfo(Integer.parseInt(shopInfoId));
			return ResultMsg.create(true, null, FastJsonUtils.toJSONString(shopShopinfo));
		}
		return ResultMsg.create(false);
	}

	/**
	 * 修改店铺信息
	 * @params shopShopInfo
	 * @return
	 */
	@RequestMapping(value="saveOrUpdateShopInfo",method=RequestMethod.POST)
	@ResponseBody
	public ResultMsg saveOrUpdateShopInfo(ShopShopInfo shopShopInfo){
		boolean flag=false;
		if(Utils.objectIsNotEmpty(shopShopInfo)){//判断是否为空
			if(Utils.objectIsNotEmpty(shopShopInfo.getShopInfoId())){
				ShopShopInfo shopInfo = shopShopInfoService.loadShopShopInfo(shopShopInfo.getShopInfoId());//查询出店铺信息
				if(Utils.objectIsNotEmpty(shopInfo)){//如果不为空，把接收到的店铺信息数据set到查询出的店铺信息
					shopInfo.setShopCategoryId(shopShopInfo.getShopCategoryId());
					shopInfo.setShopName(shopShopInfo.getShopName());
					shopInfo.setBusinessType(shopShopInfo.getBusinessType());
					shopInfo.setPostCode(shopShopInfo.getPostCode());
					shopInfo.setMainProduct(shopShopInfo.getMainProduct());
					shopInfo.setEmail(shopShopInfo.getEmail());
					shopInfo.setPhone(shopShopInfo.getPhone());
					shopInfo.setLogoUrl(shopShopInfo.getLogoUrl());
					shopInfo.setBannerUrl(shopShopInfo.getBannerUrl());
					shopInfo.setBusinessHoursStart(shopShopInfo.getBusinessHoursStart());
					shopInfo.setBusinessHoursEnd(shopShopInfo.getBusinessHoursEnd());
					shopInfo.setMarketBrand(shopShopInfo.getMarketBrand());
					shopInfo.setMarketBrandUrl(shopShopInfo.getMarketBrandUrl());
					shopInfo.setShopInfoCheckStatus(shopShopInfo.getIsPass());
					shopInfo.setSynopsis(shopShopInfo.getSynopsis());
					shopInfo.setTemplateSet(shopShopInfo.getTemplateSet());
					shopShopInfoService.updateShopShopInfo(shopInfo);//修改店铺信息
					flag=true;
				}
			}
		}
		return ResultMsg.create(flag);
	}

	/**
	 * 修改店铺企业信息
	 * @param shopShopInfo
	 * @return
	 */
	@RequestMapping(value="saveOrUpdateEnterprise",method=RequestMethod.POST)
	@ResponseBody
	public ResultMsg saveOrUpdateEnterprise(ShopShopInfo shopShopInfo){
		boolean flag=false;
		if(Utils.objectIsNotEmpty(shopShopInfo)){
			if(Utils.objectIsNotEmpty(shopShopInfo.getShopInfoId())){
				ShopShopInfo shopInfo = shopShopInfoService.loadShopShopInfo(shopShopInfo.getShopInfoId());
				if(Utils.objectIsNotEmpty(shopInfo)){
					shopInfo.setCompanyDocuments(shopShopInfo.getCompanyDocuments());
					shopInfo.setCompanyRegistered(shopShopInfo.getCompanyRegistered());
					shopInfo.setLegalOwner(shopShopInfo.getLegalOwner());
					shopInfo.setIDCards(shopShopInfo.getIDCards());
					shopInfo.setCompanyCertification(shopShopInfo.getCompanyCertification());
					shopInfo.setTaxRegistrationDocuments(shopShopInfo.getTaxRegistrationDocuments());
					shopInfo.setIDCardsImage(shopShopInfo.getIDCardsImage());
					shopInfo.setBusinessLicense(shopShopInfo.getBusinessLicense());
					shopInfo.setPostCode(shopShopInfo.getPostCode());
					shopShopInfoService.updateShopShopInfo(shopInfo);
					flag=true;
				}
			}
		}
		return ResultMsg.create(flag);
	}

	/**
	 * 进入自营店铺内商品信息
	 * @param param
	 * @return
	 */
	@RequestMapping("gotoSelfSupportShopProductListPage")
	public String gotoSelfSupportShopProductListPage(Map<String, Object> param,String shopInfoId){
		param.put("shopInfoId", shopInfoId);
		return basePath+"selfSupportShopProductList";
	}

	/**
	 * 根据店铺ID查询该店铺的商品
	 * @param page
	 * @param productName	商品名称
	 * @param sku			商品
	 * @param isPass		审核状态
	 * @param isPutSale		是否上架
	 */
	@RequestMapping("queryProductList")
	@ResponseBody
	public void queryProductList(Page page,String shopInfoId,String productName,String sku,Integer isPass,Integer isPutSale, HttpServletResponse response){
		Map<String, Object> param = new HashMap<>();
		if(Utils.objectIsNotEmpty(shopInfoId)){
			param.put("shopInfoId",shopInfoId);
		}
		if(Utils.objectIsNotEmpty(productName)){
			param.put("productName",productName.trim());
		}
		if(Utils.objectIsNotEmpty(sku)){
			param.put("sku",sku.trim());
		}
		if(Utils.objectIsNotEmpty(isPass)){
			param.put("isPass",isPass);
		}
		if(Utils.objectIsNotEmpty(isPutSale)){
			param.put("isPutSale",isPutSale);
		}
		param.put("sortColumns", "productId desc");
		int totalRecordCount =shopProductInfoService.countShopProductInfo(param);
		page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
		List<ShopProductInfo> productInfoList =shopProductInfoService.queryProductList(param, page.getPageRecordBeginIndex(), page.getPageSize());
		param.clear();
		param.put("total", totalRecordCount);// total键 存放总记录数，必须的
		param.put("rows", productInfoList);// rows键 存放每页记录 list
		ResultMsg.create(param, response);  //把内容发送到页面
	}

}
